//
//  Common.m
//  EUCIOS
//
//  Created by Khang on 3/29/13.
//  Copyright (c) 2013 Khang. All rights reserved.
//

#import "Common.h"
#import "MBProgressHUD.h"

static Common* gCommon = nil;

@interface Common () <MBProgressHUDDelegate>
@property (nonatomic, strong) MBProgressHUD* progressHUD;
@end

@implementation Common

- (id)init {
    if (self = [super init]) {
        
    }
    return self;
}

+ (id)sharedCommon {
    if (!gCommon) {
        gCommon = [[Common alloc] init];
    }
    return gCommon;
}

- (void)dealloc {
    self.progressHUD = nil;
}

- (NSInteger)objectToInterger:(id)object {
	
	if (object == nil) {
		return 0;
	}
	
	if ([object isKindOfClass:[NSString class]] ||
		[object isKindOfClass:[NSDecimalNumber class]]) {
		return [object intValue];
	} else {
        if ([object isKindOfClass:[NSNumber class]]) {
            return [object integerValue];
        }
    }
    
	return 0;
}

- (long long)objectToLong:(id)object {
	
	if (object == nil) {
		return 0;
	}
	
	if ([object isKindOfClass:[NSString class]]){
		long long lngValue = [object longLongValue];
		return lngValue;
	}
	
	if ([object isKindOfClass:[NSDecimalNumber class]]) {
		long long lngValue = [object longLongValue];
		return lngValue;
	}
	
	return 0;
}

- (NSTimeInterval)objectToTimeInterval:(id)object {
	if (object == nil) {
		return 0;
	}
	
	if ([object isKindOfClass:[NSString class]] ||
		[object isKindOfClass:[NSDecimalNumber class]]) {
		return [object doubleValue];
	}
	
	return 0;
}

- (NSString*)objectToString:(id)object {
	if (object == nil) {
		return nil;
	}
	
	if ([object isKindOfClass:[NSString class]]) {
		return object;
	} else if ([object isKindOfClass:[NSDecimalNumber class]]) {
		return [object stringValue];
	} else if ([object isKindOfClass:[NSNumber class]]) {
        return [object stringValue];
    }
	
	return nil;
}

- (NSString*)nullObjectToEmtpyString:(id)object {
	
	if (!object) {
		return @"";
	}
	
	if ([object isKindOfClass:[NSString class]]) {
		return object;
	}
    else if ([object isKindOfClass:[NSDecimalNumber class]]) {
		return [object stringValue];
	}
	
	return @"";
}

- (NSString*)nullObjectToSpaceString:(id)object {
	if (!object) {
		return @" ";
	}
	
	if ([object isKindOfClass:[NSString class]]) {
		return object;
	}
    else if ([object isKindOfClass:[NSDecimalNumber class]]) {
		return [object stringValue];
	}
	
	return object;
}

- (CGFloat)objectToFloat:(id)object {
	if (object == nil) {
		return 0.0f;
	}
	
	if ([object isKindOfClass:[NSString class]] ||
		[object isKindOfClass:[NSDecimalNumber class]]) {
		return [object floatValue];
	}
	
	return 0.0f;
}

- (BOOL)validateUrl:(NSString*)candidate {
    NSString *urlRegEx = @"(http|https)://((\\w)*|([0-9]*)|([-|_])*)+([\\.|/]((\\w)*|([0-9]*)|([-|_])*))+";
    NSPredicate *urlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", urlRegEx];
    return [urlTest evaluateWithObject:candidate];
}

#pragma mark -
#pragma mark MBProgressHUDDelegate
- (void)hudWasHidden:(MBProgressHUD *)hud {
    hud.delegate = nil;
    [hud removeFromSuperview];
    hud = nil;
}

#pragma mark -
#pragma mark Public
- (void)showMessageBox:(NSString *)title withMessage:(NSString *)message withCancelButton:(NSString *)cancel {
    UIAlertView* inform = [[UIAlertView alloc] initWithTitle:title
                                                     message:message
                                                    delegate:nil
                                           cancelButtonTitle:cancel
                                           otherButtonTitles:nil];
    [inform show];
    inform = nil;
}

- (void)showProgress:(NSString*)title withDescription:(NSString*)description inView:(UIView*)theview {
    self.progressHUD = nil;
    self.progressHUD = [[MBProgressHUD alloc] initWithView:theview];
    [theview addSubview:self.progressHUD];
    
    self.progressHUD.delegate = self;
    self.progressHUD.labelText = title;
    self.progressHUD.detailsLabelText = description;
    self.progressHUD.dimBackground = YES;
    
    [self.progressHUD show:YES];
}

- (void)hideProgress {
    if (self.progressHUD) {
        [self.progressHUD hide:YES];
    }
}

- (void)hideProgressWithDelay:(NSTimeInterval)delay {
    if (self.progressHUD) {
        [self.progressHUD hide:YES afterDelay:delay];
    }
}


#pragma mark - AppInfo
- (void)saveAppInfo:(NSMutableDictionary *)info {
    NSUserDefaults* userdefault = [NSUserDefaults standardUserDefaults];
    NSString* key = @"app_info";
    
    if (info && [info isKindOfClass:[NSMutableDictionary class]]) {
        [userdefault setObject:info forKey:key];
    } else {
        [userdefault removeObjectForKey:key];
    }
    
    [userdefault synchronize];
}

- (NSMutableDictionary *)getAppInfo {
    NSUserDefaults* userdefault = [NSUserDefaults standardUserDefaults];
    NSString* key = @"app_info";
    
    if ([userdefault objectForKey:key]) {
        NSMutableDictionary* info = [[userdefault dictionaryForKey:key] mutableCopy];
        return info;
    }
    
    return nil;
}

@end
